package com.we.flink.userlabelplatform.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.userlabelplatform.table.TableBiguoCardRiskTag;

public class TagBiguoCardRiskTag {
    public long uid;
    public int update_time;
    public int biguo_card_tag_status;
    public int biguo_card_expired_time;
    public String biguo_card_user_level;

    public TagBiguoCardRiskTag() {}

    public TagBiguoCardRiskTag(
            long uid,
            int update_time,
            int biguo_card_tag_status,
            int biguo_card_expired_time,
            String biguo_card_user_level) {
        this.uid = uid;
        this.update_time = update_time;
        this.biguo_card_tag_status = biguo_card_tag_status;
        this.biguo_card_expired_time = biguo_card_expired_time;
        this.biguo_card_user_level = biguo_card_user_level;
    }

    public static TagBiguoCardRiskTag init(long uid) {
        return new TagBiguoCardRiskTag(uid, 0, 0, 0, "");
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableBiguoCardRiskTag.QUERYSQL);
        DataStream<Tuple2<Boolean, TableBiguoCardRiskTag>> inputStream =
                tableEnv.toRetractStream(tableRes, TableBiguoCardRiskTag.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableBiguoCardRiskTag>>() {
                                    @Override
                                    public boolean filter(
                                            Tuple2<Boolean, TableBiguoCardRiskTag> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("TagBiguoCardRiskTag-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableBiguoCardRiskTag>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableBiguoCardRiskTag> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableBiguoCardRiskTag>, String>() {
                                    ValueState<TagBiguoCardRiskTag> biguoCardRiskTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        biguoCardRiskTagVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagBiguoCardRiskTag>(
                                                                        "biguoCardRiskTagVState",
                                                                        TagBiguoCardRiskTag.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableBiguoCardRiskTag> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int update_time =
                                                (int) (value.f1.getUpdate_time().getTime() / 1000);
                                        int tag_status = value.f1.getTag_status();
                                        int expired_time = value.f1.getExpired_time();
                                        String user_level = value.f1.getUser_level();

                                        TagBiguoCardRiskTag tmpState =
                                                biguoCardRiskTagVState.value();
                                        TagBiguoCardRiskTag curState =
                                                tmpState == null
                                                        ? TagBiguoCardRiskTag.init(uid)
                                                        : tmpState;

                                        if (update_time > curState.getUpdate_time()) {
                                            if (tag_status != curState.getBiguo_card_tag_status()
                                                    || expired_time
                                                            != curState.getBiguo_card_expired_time()
                                                    || !user_level.equals(
                                                            curState.getBiguo_card_user_level())) {
                                                curState.update_time = update_time;
                                                curState.biguo_card_tag_status = tag_status;
                                                curState.biguo_card_expired_time = expired_time;
                                                curState.biguo_card_user_level = user_level;

                                                JSONObject jsonObject = new JSONObject();
                                                jsonObject.put("uid", uid);
                                                jsonObject.put("biguo_card_tag_status", tag_status);
                                                jsonObject.put(
                                                        "biguo_card_expired_time", expired_time);
                                                jsonObject.put("biguo_card_user_level", user_level);

                                                out.collect(jsonObject.toString());
                                                biguoCardRiskTagVState.update(curState);
                                            }
                                        }
                                    }
                                });
        return out;
    }

    public int getUpdate_time() {
        return update_time;
    }

    public int getBiguo_card_tag_status() {
        return biguo_card_tag_status;
    }

    public int getBiguo_card_expired_time() {
        return biguo_card_expired_time;
    }

    public String getBiguo_card_user_level() {
        return biguo_card_user_level;
    }

    @Override
    public String toString() {
        return "TagBiguoCardRiskTag{"
                + "uid="
                + uid
                + ", update_time="
                + update_time
                + ", biguo_card_tag_status="
                + biguo_card_tag_status
                + ", biguo_card_expired_time="
                + biguo_card_expired_time
                + ", biguo_card_user_level='"
                + biguo_card_user_level
                + '\''
                + '}';
    }
}
